#include "State_Menu.h"
//#include "State_GamePlay.h"

extern IDirectInputDevice8		*mJoystick[2]; // This will be changed to an area later...
extern DIJOYSTATE2				js;
//extern 	int prevJSButtons[12];
//extern 	int prevJSButtons2[12];
//extern 	int prevJS;
extern 	ID3DXSprite* mSprite;

State_Menu* State_Menu::init = 0;

State_Menu::State_Menu()
{
	HR(D3DXCreateTextureFromFile(gd3dDevice, "MazeMenuTest01.png", &mBkgdTex));
	mBkgdCenter = D3DXVECTOR3(640.0f, 400.0f, 0.0f);
	MenuViewPort.X	   = 0;
	MenuViewPort.Y	   = 0;
	MenuViewPort.Width   = 1280;
	MenuViewPort.Height  = 768;
	MenuViewPort.MinZ	   = 0.0f;
	MenuViewPort.MaxZ	   = 0.0f;
	//gd3dDevice->SetViewport( &MenuViewPort );
}

void State_Menu::Enter()
{
//	cout << "State_Menu has been entered\n";
	choice = '0';
}

void State_Menu::Execute(D3DApp* g)
{
			
	static float cursorDelay = 0.0f;
	static float timeAccum = 0.0f;
	static int prevJS = 0;
	mJoystick[0]->GetDeviceState(sizeof(DIJOYSTATE2), &js);
		
	if(((js.rgdwPOV[0] != -1) && js.rgdwPOV[0] == 0) && (js.rgdwPOV[0] ^ prevJS))
	{
		//MessageBox(gd3dApp->getMainWindowHandle(), "1st", "Error", 0);
		MessageBox(gd3dApp->getMainWindowHandle(), "1st", "Error", 0);
	}

	else if(((js.rgdwPOV[0] != -1) && js.rgdwPOV[0] == 9000) && (js.rgdwPOV[0] ^ prevJS))
	{
		//MessageBox(gd3dApp->getMainWindowHandle(), "2nd", "Error", 0);
	}

	else if(((js.rgdwPOV[0] != -1) && js.rgdwPOV[0] == 18000) && (js.rgdwPOV[0] ^ prevJS))
	{
		//MessageBox(gd3dApp->getMainWindowHandle(), "3rd", "Error", 0);
	}

	else if(((js.rgdwPOV[0] != -1) && js.rgdwPOV[0] == 27000) && (js.rgdwPOV[0] ^ prevJS))
	{
		//MessageBox(gd3dApp->getMainWindowHandle(), "4th", "Error", 0);
	}
	else if((js.rgbButtons[0] & 0x80) && (prevJSButtons[0] ^ js.rgbButtons[0]))
	{
		gd3dApp->setState(State_Single::Instance());
	}

	else if(js.rgbButtons[1] & 0x80 && (prevJSButtons[1] ^ js.rgbButtons[1]))
	{
		gd3dApp->setState(TwoPlayer::Instance());
	}

	else if(js.rgbButtons[2] & 0x80 && (prevJSButtons[2] ^ js.rgbButtons[2]))
	{
//		gd3dApp->setState(FourPlayer::Instance());
	}

	else if(js.rgbButtons[3] & 0x80 && (prevJSButtons[3] ^ js.rgbButtons[3]))
	{
		//PostQuitMessage(0);
	}
	else if(js.rgbButtons[4] & 0x80 && (prevJSButtons[4] ^ js.rgbButtons[4]))
	{
		PostQuitMessage(0);
	}

	if(!(js.rgdwPOV[0] == 4500 || js.rgdwPOV[0] == 13500 || js.rgdwPOV[0] == 22500 || js.rgdwPOV[0] == 31500))
			prevJS = js.rgdwPOV[0];

	for(int i = 0; i < 12; i++)
	{
		prevJSButtons[i] = js.rgbButtons[i];
	}

	Draw();
}

void State_Menu::Exit()
{
//	cout << "Exiting State_Menu\n";
}

void State_Menu::Draw()
{
	gd3dDevice->SetViewport( &MenuViewPort );

    // Now we can clear just view-port's portion of the buffer to red...
   HR(gd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0));

    HR(gd3dDevice->BeginScene());
    
    //draw stuff here
	HR(mSprite->Begin(D3DXSPRITE_OBJECTSPACE | D3DXSPRITE_DONOTMODIFY_RENDERSTATE));

	D3DXMATRIX texScaling;
	D3DXMatrixScaling(&texScaling, 1.0f, -1.0f, 0.0f);
	HR(gd3dDevice->SetTransform(D3DTS_TEXTURE0, &texScaling));

	D3DXMATRIX T, S;
	D3DXMatrixTranslation(&T, -260.0f, -200.0f, 0.0f);
	D3DXMatrixScaling(&S, 0.68f, 1.0f, 0.0f);
	HR(mSprite->SetTransform(&(S*T)));

	//draw the menu
	HR(mSprite->Draw(mBkgdTex, 0, &mBkgdCenter, 0, D3DCOLOR_XRGB(255, 255, 255)));
	HR(mSprite->Flush());

	//restore the texture coordinate scaling transform. mirror texture 
	//coordinates with respect to y-axis so the texture is mapped correctly onto the sprite
	D3DXMatrixScaling(&texScaling, 1.0f, -1.0f, 0.0f);
	HR(gd3dDevice->SetTransform(D3DTS_TEXTURE0, &texScaling));

	//anything that needs to be drawn should be above this line and below the mSprite->Begin line
	HR(mSprite->End());

	HR(gd3dDevice->EndScene());
	


	//present the back buffer
	HR(gd3dDevice->Present(0, 0, 0, 0));
}

State_Menu* State_Menu::Instance()
{
	if(init == 0)
	{
		init = new State_Menu();
	}
	return init;
}
